package com.wgchao.authmanager.infrastructure.convert.ev;

import com.wgchao.authmanager.application.vo.AdministratorVo;
import com.wgchao.authmanager.infrastructure.convert.Convert;
import com.wgchao.authmanager.infrastructure.jpa.entity.AgencyInfo;
import com.wgchao.authmanager.infrastructure.jpa.entity.UserInfo;
import com.wgchao.authmanager.infrastructure.jpa.repository.AgencyInfoJpaRepository;
import com.wgchao.authmanager.infrastructure.jpa.repository.UserInfoJpaReposotory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @ClassName: UserInfoConvertVo
 * @author: wgchao
 * @createTime: 2020/5/7 11:11 AM
 * @Description: TODO
 */
@Component
public class UserInfoConvertVo implements Convert<AdministratorVo, UserInfo> {
    @Autowired
    private UserInfoJpaReposotory userInfoJpaReposotory;
    @Autowired
    private AgencyInfoJpaRepository agencyInfoJpaRepository;

    @Override
    public UserInfo toEntity(AdministratorVo administratorVo) {
        return null;
    }

    @Override
    public AdministratorVo toModel(UserInfo userInfo) {
        AdministratorVo administratorVo = new AdministratorVo();
        BeanUtils.copyProperties(userInfo, administratorVo);
        UserInfo creator = userInfoJpaReposotory.findFirstByUname(userInfo.getCreateUser());
        if(creator != null){
            administratorVo.setCreatorName(creator.getName());
        }
        if(!StringUtils.isEmpty(userInfo.getLastUpdateUser())){
            UserInfo updator = userInfoJpaReposotory.findFirstByUname(userInfo.getLastUpdateUser());
            if(updator != null){
                administratorVo.setLastUpdatorName(updator.getName());
            }
        }
        AgencyInfo agencyInfo = agencyInfoJpaRepository.findFirstByAgencyCode(userInfo.getAgencyCode());
        administratorVo.setAgencyName(agencyInfo.getAgencyName());

        return administratorVo;
    }
}
